package com.test.daily.leetcode.y2022.m06.day0610.v02;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/6/10 下午 01:36
 * @version: 1.0
 */
public class Solution {
    public static int minPathSum1(int[][] m) {
        if (m == null || m.length == 0 || m[0] == null || m[0].length == 0) {
            return 0;
        }
        int col = m.length;
        int row = m[0].length;
        int[][] dp = new int[row][col];
        dp[0][0] = m[0][0];
        for (int i = 1; i < col; i++) {
            dp[0][i] = dp[0][i - 1] + m[0][i];
        }
        for (int i = 0; i < row; i++) {
            dp[i][0] = dp[i - 1][0] + m[i][0];
        }
        for (int i = 1; i < row; i++) {
            for (int j = 1; j < col; j++) {
                dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + m[i][j];
            }
        }
        return dp[col - 1][row - 1];
    }
    public static int minPathSum2(int[][] m){
        if(m == null || m.length == 0 || m[0] == null || m[0].length == 0){
            return 0;
        }

        int col = m[0].length;
        int row = m.length;
        int dp[] = new int[col];
        dp[0] = m[0][0];
        for(int i = 1; i < col; i++){
            dp[i] = dp[i-1] + m[0][i];
        }

        for(int i = 1; i < row; i++){
            dp[0] += m[i][0];
            for(int j = 1; j < col; j++){
                dp[j] = Math.min(dp[j], dp[j-1]) + m[row][col];
            }
        }
        return dp[col - 1];
    }
}
